package red.social;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.ByteArrayBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;

public class CamaraView extends Activity implements SurfaceHolder.Callback,OnClickListener {
	static final int FOTO_MODE = 0;
	private static final String TAG = "CameraTest";
	Camera mCamera;
	boolean mPreviewRunning = false;
	private Context mContext = this;
	
	
	

	@Override
		public void onCreate(Bundle icicle) {
			super.onCreate(icicle);



			requestWindowFeature(Window.FEATURE_NO_TITLE);
			getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
					WindowManager.LayoutParams.FLAG_FULLSCREEN);
			
			
			setContentView(R.layout.camaralayout);
			
			
			mSurfaceView = (SurfaceView) findViewById(R.id.surface_camera);
			
			mSurfaceView.setOnClickListener(this);
			
			mSurfaceHolder = mSurfaceView.getHolder();
			mSurfaceHolder.addCallback(this);
			mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
		}

		@Override
		protected void onRestoreInstanceState(Bundle savedInstanceState) {
			super.onRestoreInstanceState(savedInstanceState);
		}

		Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
			public void onPictureTaken(byte[] imageData, Camera c) {

				if (imageData != null) {

					Intent mIntent = new Intent();

					StoreByteImage(mContext, imageData, 50, "ImageName");
					mCamera.startPreview();

					setResult(FOTO_MODE, mIntent);
					finish();

				}
			}
		};

		protected void onResume() {
			Log.e(TAG, "onResume");
			super.onResume();
		}

		protected void onSaveInstanceState(Bundle outState) {
			super.onSaveInstanceState(outState);
		}

		protected void onStop() {
			Log.e(TAG, "onStop");
			super.onStop();
		}

		public void surfaceCreated(SurfaceHolder holder) {
			Log.e(TAG, "surfaceCreated");
			mCamera = Camera.open();

		}

		public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
			Log.e(TAG, "surfaceChanged");

			// XXX stopPreview() will crash if preview is not running
			if (mPreviewRunning) {
				mCamera.stopPreview();
			}

			try {
				mCamera.setPreviewDisplay(holder);
			} catch (IOException e) {
				e.printStackTrace();
			}
			mCamera.startPreview();
			mPreviewRunning = true;
		}

		public void surfaceDestroyed(SurfaceHolder holder) {
			Log.e(TAG, "surfaceDestroyed");
			mCamera.stopPreview();
			mPreviewRunning = false;
			mCamera.release();
		}

		private SurfaceView mSurfaceView;
		private SurfaceHolder mSurfaceHolder;

		public void onClick(View arg0) {

			mCamera.takePicture(null, mPictureCallback, mPictureCallback);

		}

		public static boolean StoreByteImage(Context mContext, byte[] imageData,
				int quality, String expName) {

			File sdImageMainDirectory = new File("/sdcard");
			FileOutputStream fileOutputStream = null;
			String nameFile;
			try {

				BitmapFactory.Options options = new BitmapFactory.Options();
				options.inSampleSize = 5;

				Bitmap myImage = BitmapFactory.decodeByteArray(imageData, 0,
						imageData.length, options);
				
				ByteArrayOutputStream bos = new ByteArrayOutputStream();
				myImage.compress(CompressFormat.JPEG, quality,bos);
				byte[] data = bos.toByteArray();

				
				HttpClient httpClient = new DefaultHttpClient();
				HttpPost postRequest = new HttpPost(
						"http://192.168.183.81:8080/Modulo10-war/ServletSubirFotos");
				ByteArrayBody bab = new ByteArrayBody(data, "camara.jpg");
				MultipartEntity reqEntity = new MultipartEntity(
						HttpMultipartMode.BROWSER_COMPATIBLE);
				reqEntity.addPart("idUsuario", new StringBody("1")); //IDUSUARIO
				reqEntity.addPart("bImagen", bab);	
				postRequest.setEntity(reqEntity);
				httpClient.execute(postRequest);
			
			

			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			return true;
		}

	}
	
	
